package com.finance.cooperate.common.utils;

import java.io.*;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

/**
 * @ClassName CSVUtils
 * @Description 写CSV文件
 * @Author shen
 * @Date 2022/5/25 16:00
 * @Modify ...
 */
public class CSVUtils {

    public static File createCSVFile(List exportData, Map rowMapper,
                                     String outPutPath, String filename) {

        File csvFile = null;
        BufferedWriter csvFileOutputStream = null;
        try {
            csvFile = new File(outPutPath + filename + ".csv");
            // csvFile.getParentFile().mkdir();
            File parent = csvFile.getParentFile();
            if (parent != null && !parent.exists()) {
                parent.mkdirs();
            }

            if (!csvFile.exists()) {
                csvFile.createNewFile();

                // GB2312使正确读取分隔符","
                csvFileOutputStream = new BufferedWriter(new OutputStreamWriter(
                        new FileOutputStream(csvFile), StandardCharsets.UTF_8));
                // 写入文件头部
                for (Iterator propertyIterator = rowMapper.entrySet().iterator(); propertyIterator.hasNext(); ) {
                    java.util.Map.Entry propertyEntry = (java.util.Map.Entry) propertyIterator
                            .next();

//                    csvFileOutputStream.write("\""
//                            + propertyEntry.getValue().toString() + "\"");


                    csvFileOutputStream.write(propertyEntry.getValue().toString());


//                    if (propertyIterator.hasNext()) {
//                        csvFileOutputStream.write(",");
//                    }

                    if (propertyIterator.hasNext()) {
                        csvFileOutputStream.write("/001XG");
                    }



                }
                csvFileOutputStream.newLine();
            }else {
                // GB2312使正确读取分隔符","
                csvFileOutputStream = new BufferedWriter(new OutputStreamWriter(
                        new FileOutputStream(csvFile,true), StandardCharsets.UTF_8));
                csvFileOutputStream.newLine();
            }


            // 写入文件内容
            for (Iterator iterator = exportData.iterator(); iterator.hasNext(); ) {
                // Object row = (Object) iterator.next();
                HashMap row = (HashMap) iterator.next();


                for (Iterator propertyIterator = row.entrySet().iterator(); propertyIterator.hasNext(); ) {
                    java.util.Map.Entry propertyEntry = (java.util.Map.Entry) propertyIterator.next();

                    // 不再进行字符串验证
                    // System.out.println( BeanUtils.getProperty(row, propertyEntry.getKey().toString()));
//                    String other=propertyEntry.getValue().toString().replaceAll("[0-9-.E]+","");
//                    if (!other.isEmpty()||!other.equals("")){
//                        System.out.println("error"+"_"+"_"+propertyEntry.getValue().toString());
//                    }


//                    csvFileOutputStream.write("\""
//                            + propertyEntry.getValue().toString().replace("\"","").replace(" ","").trim() + "\"");


                    csvFileOutputStream.write(propertyEntry.getValue().toString().trim());

                    if (propertyIterator.hasNext()) {
                        csvFileOutputStream.write("/001XG");
                    }


//                    if (propertyIterator.hasNext()) {
//                        csvFileOutputStream.write(",");
//                    }
                }
                if (iterator.hasNext()) {
                    csvFileOutputStream.newLine();
                }
            }
            csvFileOutputStream.flush();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                csvFileOutputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return csvFile;
    }

}
